package com.hbpu.maven.spring.ioc.processor;

import com.hbpu.maven.spring.ioc.annotation.UUID;
import com.hbpu.maven.spring.ioc.bean.Car;
import com.hbpu.maven.spring.ioc.bean.Person;
import com.hbpu.maven.spring.ioc.bean.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class TestProcess implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Car) {
            Class<?> aClass = bean.getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                if (declaredField.isAnnotationPresent(UUID.class)) {
                    declaredField.setAccessible(true);
                    try {
                        declaredField.set(bean, java.util.UUID.randomUUID().toString());
                    } catch (IllegalAccessException e) {
                        System.out.println("使用反射修改值的时候出错了：" + e.getMessage());
                    }
                }
            }
        }
        return bean;
    }
}
